<?
function createDisplayRound($round) {
	$currentRound = getCurrentRound();
    $currentRoundStatus = getCurrentRoundStatus();
	
	$isCurrent = false;
	if ($currentRound == $round) {
		$isCurrent = true;
	}
	
	$leagues = getActiveLeagues();
	foreach ($leagues as $league) {	
		$fixture = getRoundResults($league, $round);
		$lastRound = -1;
		
		$results = "";
		$results .= "<link type=\"text/css\" href=\"../../../css/domTT.css\" rel=\"stylesheet\" />";
		$results .= "<link type=\"text/css\" href=\"../../../css/simpleToolTip.css\" rel=\"stylesheet\" />";

		$results .= "<script type=\"text/javascript\" src=\"../../../js/domTT/alphaAPI.js\"></script>";
		$results .= "<script type=\"text/javascript\" src=\"../../../js/domTT/domLib.js\"></script>";
		$results .= "<script type=\"text/javascript\" src=\"../../../js/domTT/domTT.js\"></script>";
		$results .= "<script type=\"text/javascript\" src=\"../../../js/domTT/domTT_drag.js\"></script>";
		$results .= "<script type=\"text/javascript\" src=\"../../../js/domTT/fadomatic.js\"></script>";
		$results .= "<script>";
		$results .= "function getPlayerInfo(playerInfo) {";
   		$results .= "if (navigator.appName == 'Microsoft Internet Explorer') {";
	    $results .= "document.write(\"<span class=\\\"SimpleTip\\\" onmouseover=\\\"javascript:this.className='SimpleTipHover'\\\" onmouseout=\\\"javascript:this.className='SimpleTip'\\\"><img src=\\\"../../../images/info.jpg\\\" width=\\\"15px\\\" height=\\\"15px\\\"><div>\" + playerInfo + \"</div></span>&nbsp;\");";
        $results .= "}";
        $results .= "else {";
        $results .= "document.write(\"<a href=\\\"#\\\" onmouseover=\\\"this.style.color = '#D17E62'; domTT_activate(this, event, 'content', '\" + playerInfo + \"', 'trail', true, 'fade', 'both', 'fadeMax', 87, 'styleClass', 'niceTitle');\\\" onmouseout=\\\"this.style.color = ''; domTT_mouseout(this, event);\\\"><img src=\\\"../../../images/info.jpg\\\" width=\\\"15px\\\" height=\\\"15px\\\"></a>&nbsp;\");"; 
        $results .= "}";
        $results .= "}";        
        $results .= "</script>";
		
		$count = count($fixture);
		$count2 = -1;
		for ($i = 0; $i < $count; $i++) {
			$homeTeamScore = $fixture[$i]->getHomeTeamScore();
			$awayTeamScore = $fixture[$i]->getAwayTeamScore();
			$versesDescription = "verses";
			if ($isCurrent && $currentRoundStatus != RoundStatusType::Open) {
				if ($homeTeamScore > $awayTeamScore) {
					$versesDescription = "defeating";
				}
				else if ($awayTeamScore > $homeTeamScore) {
					$versesDescription = "being defeated by";
				}
				else {
					$versesDescription = "deadlocked";
				}
			}
			else if ($currentRound > $round) {
				if ($homeTeamScore > $awayTeamScore) {
					$versesDescription = "defeated";
				}
				else if ($awayTeamScore > $homeTeamScore) {
					$versesDescription = "defeated by";
				}
				else {
					$versesDescription = "drew with";
				}			
			}
			if ($fixture[$i]->round != $lastRound) {
				$lastRound = $fixture[$i]->round;
				$results .= "<tr>";
				$results .= "	<td colspan=\"5\" align=\"center\"><h2>Round " . $fixture[$i]->round . "</td>"; 
				$results .= "</tr>";
			}
			$results .= "<tr>";
			
			$results .= "<td width=\"20%\">" . $fixture[$i]->homeTeamName . "</td><td width=\"20%\">" . $homeTeamScore . " (" . $fixture[$i]->getHomePlayersPlayed() . " played)</td>";
			$results .= "<td width=\"20%\"><b>" . $versesDescription . "</b></td>";
			$results .= "<td width=\"20%\">" . $fixture[$i]->awayTeamName . "</td><td width=\"20%\">" . $awayTeamScore . " (" . $fixture[$i]->getAwayPlayersPlayed() . " played)</td>";
			$results .= "</tr>";
		}
		$results .= "</table><br><br>";
		$results .= "<table width=\"60%\" align=\"center\">";
		for ($i = 0; $i < $count; $i++) {
			$results .= "<tr><td><h2>" . $fixture[$i]->homeTeamName . "</h2></td><td><h2>" . $fixture[$i]->awayTeamName . "</h2></td</tr>";
			$results .= "<tr><td width=\"50%\" valign=\"top\"><table>";
			$replacementString = "";
			$count2 = count($fixture[$i]->homePlayerScores->playerScores);
			for ($j = 0; $j < $count2; $j++) {
				if ($fixture[$i]->homePlayerScores->playerScores[$j]->replacementFor != null) {
					$replacementString .= "<tr><td colspan=2><i>" . getPlayerDisplayForPlayer($fixture[$i]->homePlayerScores->playerScores[$j]->player) . " <br>replacement for " . getPlayerDisplayForPlayer($fixture[$i]->homePlayerScores->playerScores[$j]->replacementFor) . "</i>";
					$replacementString .= "&nbsp;<script>getPlayerInfo('Replacement Description<p>" . $fixture[$i]->homePlayerScores->playerScores[$j]->replacementDescription . "</p>');</script>";
					$replacementString .= "</td></tr>";
				}
				$results .= "<tr><td>" . getPlayerDisplayForPlayer($fixture[$i]->homePlayerScores->playerScores[$j]->player) . "</td><td>" . $fixture[$i]->homePlayerScores->playerScores[$j]->score . "</td></tr>";				
			}
			$results .= $replacementString;
			$results .= "<tr><td><hr></td></tr>";
			$count2 = count($fixture[$i]->homePlayerScores->nonScoringPlayers);
			for ($j = 0; $j < $count2; $j++) {
				if ($fixture[$i]->homePlayerScores->nonScoringPlayers[$j] != null && $fixture[$i]->homePlayerScores->nonScoringPlayers[$j]->score > 0  && !$fixture[$i]->homePlayerScores->nonScoringPlayers[$j]->traded) {				
					$results .= "<tr><td>" . getPlayerDisplayForPlayer($fixture[$i]->homePlayerScores->nonScoringPlayers[$j]->player) . "</td><td>" . $fixture[$i]->homePlayerScores->nonScoringPlayers[$j]->score;
					if ($fixture[$i]->homePlayerScores->nonScoringPlayers[$j]->emergency) {
						$results .= "&nbsp;<img src=\"../../../images/red-e.JPG\" />";
					}
					$results .= "</td></tr>";				
				}
			}			
			$results .= "</table><br><br></td>";
			$results .= "<td width=\"50%\" valign=\"top\"><table>";
			$replacementString = "";
			$count2 = count($fixture[$i]->awayPlayerScores->playerScores);
			for ($j = 0; $j < $count2; $j++) {
				if ($fixture[$i]->awayPlayerScores->playerScores[$j]->replacementFor != null) {
					$replacementString .= "<tr><td><i>" . getPlayerDisplayForPlayer($fixture[$i]->awayPlayerScores->playerScores[$j]->player) . " <br>replacement for " . getPlayerDisplayForPlayer($fixture[$i]->awayPlayerScores->playerScores[$j]->replacementFor) . "</i>";
					$replacementString .= "&nbsp;<script>getPlayerInfo('Replacement Description<p>" . $fixture[$i]->awayPlayerScores->playerScores[$j]->replacementDescription . "</p>');</script>";
					$replacementString .= "</td></tr>"; 		
				}
				$results .= "<tr><td>" . getPlayerDisplayForPlayer($fixture[$i]->awayPlayerScores->playerScores[$j]->player) . "</td><td>" . $fixture[$i]->awayPlayerScores->playerScores[$j]->score . "</td></tr>";				
			}
			$results .= $replacementString;
			$results .= "<tr><td><hr></td></tr>";
			$count2 = count($fixture[$i]->awayPlayerScores->nonScoringPlayers);
			for ($j = 0; $j < $count2; $j++) {
				if ($fixture[$i]->awayPlayerScores->nonScoringPlayers[$j] != null && !$fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->traded) {
					if (($fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->score >= 0 && $fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->emergency) || $fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->score > 0) {
						$results .= "<tr><td>" . getPlayerDisplayForPlayer($fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->player) . "</td><td>" . $fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->score;
						if ($fixture[$i]->awayPlayerScores->nonScoringPlayers[$j]->emergency) {
							$results .= "&nbsp;<img src=\"../../../images/red-e.JPG\" />";
						}
						$results .= "</td></tr>";
					}				
				}
			}			
			$results .= "</table><br><br></td></tr>";
		}


		
		$file = getPathPrefix() . "/html/round" . $round . "-" . $league . ".htm"; 
	 	$handle = fopen($file, 'w');
	 	fwrite($handle, $results); 
	 	fclose($handle);
	} 
}

function getPathPrefix() {
	$pathPrefix = "..";
	if (PHP_OS == "Linux") {
		$pathPrefix = "/var/www/masoCoach/phpMain/pages";
	}
	
	return $pathPrefix;
}

function createFixture() {
	$leagues = getActiveLeagues();
	foreach ($leagues as $league) {	
		$fixture = getCurrentFixture($league);
		$result = "<br><br><br>";
		$result .= "<table align=\"center\" width=\"60%\"><tr>";
		$result .= displayRoundForFixture($fixture);
		$result .= "</table>";
		
		$file = getPathPrefix() . "/html/fixture-" . $league . ".htm"; 
	 	$handle = fopen($file, 'w');
	 	fwrite($handle, $result); 
	 	fclose($handle);
	}
}

function createLadder() {
	$leagues = getActiveLeagues();
	foreach ($leagues as $league) {
		$result = "<table width=\"100%\">\n";
		$result .= "<tr>\n";
		$result .= "<td width=\"20%\"><b>Team name</b></td>\n";
		$result .= "<td width=\"10%\"><b>Won</b></td>\n";
		//$result .= "<td width=\"10%\"><b>Drawn</b></td>\n";
		$result .= "<td width=\"10%\"><b>For</b></td>\n";
		$result .= "<td width=\"10%\"><b>Against</b></td>\n";
		$result .= "<td width=\"10%\"><b>%</b></td>\n";
		$result .= "<td width=\"10%\"><b>Highest</b></td>\n";
		$result .= "<td width=\"10%\"><b>Lowest</b></td>\n";
		$result .= "<td width=\"10%\"><b>Average</b></td>\n";
		$result .= "<td width=\"10%\"><b>Streak</b></td>\n";
		$result .= "</tr>\n";
		$coachResults = getLadder($league);
		$count = count($coachResults);
		for ($i = 0; $i < $count; $i++) {
			$result .= "<tr>\n";
			$result .= "<td width=\"20%\">" . $coachResults[$i]->team->teamName . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->gamesWon . "</td>\n";
			//$result .= "<td width=\"10%\">" . $coachResults[$i]->gamesDrawn . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->forValue . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->againstValue . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->getPercentage() . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->highestScore . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->lowestScore . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->getAverageScore() . "</td>\n";
			$result .= "<td width=\"10%\">" . $coachResults[$i]->getStreakVal() . "</td>\n";
			$result .= "</tr>\n";
			if (($i == 7 && $count == 16) || ($i == 5 && $count == 12)) {
				$result .= "<tr>\n";
				$result .= "<td colspan=9><hr></td>\n";
				$result .= "</tr>\n";
			}
		}
	
		$result .= "</table>";
	
		$file = getPathPrefix() . "/html/ladder-" . $league . ".htm"; 
	 	$handle = fopen($file, 'w');
	 	fwrite($handle, $result); 
	 	fclose($handle);
	}
}

?>